package HP.p0828;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

public class TestNew {

    public static void main(String[] args) throws Exception {
        TestNew t = new TestNew();
        // t.add();
        // t.update();
        // t.delete();
        // t.query();
        t.login2();
    }

    public void add() throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入编号");
        String n = input.nextLine();
        System.out.print("请输入姓名");
        String name = input.nextLine();
        System.out.print("请输入密码");
        String pwd = input.nextLine();

        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");

        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger";

        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);

        // 4 创建SQL语句
        String sql = "insert into userinfo values(" + n + ",'" + name + "','" + pwd + "')";

        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();

        // 6 发送并执行
        int r = st.executeUpdate(sql);

        if (r > 0) {
            System.out.println("保存成功");
        } else {
            System.out.println("保存失败");
        }
        // 7 关闭连接
        st.close();
        conn.close();
    }

    public void update() throws Exception {

        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");

        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger";

        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);

        // 4 创建SQL语句
        String sql = "update userinfo set userpwd='666' where userid=1 ";

        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();

        // 6 发送并执行
        int r = st.executeUpdate(sql);

        if (r > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        // 7 关闭连接
        st.close();
        conn.close();
    }

    public void delete() throws Exception {

        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");

        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger";

        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);

        // 4 创建SQL语句
        String sql = "delete from Userinfo where userid=3";

        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();

        // 6 发送并执行
        int r = st.executeUpdate(sql);

        if (r > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        // 7 关闭连接
        st.close();
        conn.close();
    }

    public void query() throws Exception {

        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger";
        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);
        // 4 创建SQL语句
        String sql = "select *from userinfo";
        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();
        // 6 执行查询操作
        ResultSet rs = st.executeQuery(sql);
        // 7 循环输出
        while (rs.next()) {
            System.out
                    .println(rs.getObject("userid") + "\t" + rs.getObject("username") + "\t" + rs.getObject("userpwd"));
        }
        // 8 关闭连接
        rs.close();
        st.close();
        conn.close();
    }

    public void login() throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入姓名:");
        String name = input.nextLine();
        System.out.print("请输入密码:");
        String pwd = input.nextLine();
        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger"; // ' or 1=1 --
        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);
        // 4 创建SQL语句
        String sql = "select *from userinfo where username='" + name + "' " + "and userpwd='" + pwd + "'";
        System.out.println(sql);

        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();

        // 6 执行查询操作
        ResultSet rs = st.executeQuery(sql);

        boolean isLoginSuccess = false;

        // 7 循环输出
        while (rs.next()) {
            isLoginSuccess = true;
        }

        // 8 关闭连接
        rs.close();
        st.close();
        conn.close();

        if (isLoginSuccess) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }

    }

    public void login2() throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入姓名:");
        String name = input.nextLine();
        System.out.print("请输入密码:");
        String pwd = input.nextLine();
        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger"; // ' or 1=1 --
        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);
        // 4 创建SQL语句
        String sql = "select *from userinfo where username=? and userpwd=?";

        // 5 创建PreparedStatement(发送sql语句到数据库中)
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, name);
        ps.setObject(2, pwd);

        // 创建ResultSet
        ResultSet rs = ps.executeQuery();

        boolean isLoginSuccess = false;

        // 7 循环输出
        while (rs.next()) {
            isLoginSuccess = true;
        }

        // 8 关闭连接
        rs.close();
        ps.close();
        conn.close();

        if (isLoginSuccess) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }

    }

}
